package cn.lysj.framework.pirpraph.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.lysj.framework.base.BaseController;
import cn.lysj.framework.common.annotation.CurrentUser;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.utils.pagination.PageVo;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.pirpraph.domain.FoodReport;
import cn.lysj.framework.pirpraph.service.FoodReportService;
import cn.lysj.framework.pirpraph.util.ExcelStyleUtil;
import cn.lysj.framework.store.domain.UserFeedback;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author drj
 * @description 菜品数量报表统计控制层
 * @create 2019-01-22 16:21:18
 **/
@Slf4j
@RestController
@Api(description = "菜品数量报表统计管理接口")
@RequestMapping("/food_report")
public class FoodReportController extends BaseController<FoodReport> {

    @Resource
    private FoodReportService foodReportService;


    @Override
    public FoodReportService getService() {
        return foodReportService;
    }


    @GetMapping("/list_rewrite")
    public Resp list(@CurrentUser User user, FoodReport foodReport, PageVo pageVo) {
        foodReport.setStoreId(user.getStoreId());
        return new Resp(foodReportService.list(foodReport, pageVo));
    }


    @GetMapping("/download_excel")
    public Resp downloadExcel(HttpServletResponse response, String storeId) throws Exception {
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + "foodReport.xls");
        response.setCharacterEncoding("utf-8");
        if (ParamUtil.isBlank(storeId)) {
            return null;
        }
        List<FoodReport> foodReportList = foodReportService.getRepository().findByStoreIdOrderByPlatNumberDesc(storeId);
        for (FoodReport foodReport : foodReportList) {
            foodReport.setTotalNumber(foodReport.getPlatNumber()+foodReport.getElmNumber()+foodReport.getMtNumber());
        }
        ExportParams exportParams = new ExportParams("菜品销量报表", "foodReport", ExcelType.XSSF);
        exportParams.setStyle(ExcelStyleUtil.class);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, FoodReport.class, foodReportList);
        Sheet sheetAt = workbook.getSheetAt(0);
        for (int i = 0; i < sheetAt.getRow(0).getPhysicalNumberOfCells(); i++) {
            sheetAt.autoSizeColumn(i);
        }
        workbook.write(response.getOutputStream());
        return new Resp(foodReportList);
    }

}
